COMBAT_OB = class("COMBAT_OB")
COMBAT_OB:inherit(F_COMN_DBASE)
COMBAT_OB:inherit(F_COMN_ID)

function COMBAT_OB:set_map_id(id)
    return self:set_temp("map_id", id)
end

function COMBAT_OB:get_map_id()
    return self:query_temp("map_id")
end

---! 加入战斗
function COMBAT_OB:join_combat(user_ob)
    local player = {}
    player.user_id = user_ob:get_id()
    player.begin_time = os.time()
    player.schedule = 0
    player.robot = user_ob:is_robot()
    self:set_temp("players", user_ob:get_id(), player)
end

function COMBAT_OB:get_dead_num()
    return self:get_temp("dead_num") or 0
end

function COMBAT_OB:add_dead_num(add)
    return self:set_temp("dead_num", self:get_dead_num() + add)
end

function COMBAT_OB:get_fighting()
    return self:get_temp("fighting")
end

function COMBAT_OB:set_fighting(fighting)
    return self:set_temp("fighting", fighting)
end

function COMBAT_OB:set_begin_time(begin_time)
    return self:set_temp("begin_time", begin_time)
end

function COMBAT_OB:get_begin_time()
    return self:get_temp("begin_time")
end

function COMBAT_OB:get_players()
    return self:get_temp("players") or {}
end

function COMBAT_OB:del_user(user_id)
    self:delete_temp("players", user_id)
end

function COMBAT_OB:set_schedule(user_id, schedule)
    return self:set_temp("players", user_id, "schedule", schedule)
end

function COMBAT_OB:get_schedule(user_id)
    return self:get_temp("players", user_id, "schedule") or 0
end

function COMBAT_OB:get_user_num()
    return self:query_temp("user_num") or 0
end

---! 获取未完成加载的ids
function COMBAT_OB:get_schedule_not_finish()
    local ids = {}
    for _id, _player in pairs(self:get_players()) do
        if _player.schedule < 10000 then
            table.insert(ids, _id)
        end
    end
    return ids
end

---! 检查所有进度条
function COMBAT_OB:check_schedule_finish()
    for _id, _player in pairs(self:get_players()) do
        if not _player.robot and _player.schedule < 10000 then
            return false
        end
    end
    return true
end

---! 获取玩家对象
function COMBAT_OB:get_player_obs(exclude_robot)
    local players = {}
    local _players = self:get_players()
    for _user_id, _ in pairs(_players) do repeat
        local user_ob = USER_D:find_user(_user_id)
        if not user_ob then
            break
        end

        if exclude_robot and user_ob:is_robot() then
            break
        end

        table.insert(players, user_ob)
    until true end
    return players
end

---! 获取指定玩家对象
function COMBAT_OB:get_player_ob(user_id)
    if not self:get_temp("players", user_id) then
        return
    end
    return USER_D:find_user(user_id)
end

---! 战斗是否已结束
function COMBAT_OB:is_finished()
    local tanks = {}
    for _, player in ipairs(self:get_player_obs()) do
        local tank = player:query_temp("tank")
        if tank and not tank:is_dead() then
            table.insert(tanks, tank)
        end
    end

    if #tanks > 1 then
        return false
    end
    return true
end

---! 销毁
function COMBAT_OB:clear_combat()
    ---! 清理玩家数据
    for _, _user_ob in ipairs(self:get_player_obs()) do
        _user_ob:clear_combat()
    end

    ---! 清理定时器
    local timer_id = self:query_temp("sync_timer_id")
    if timer_id then
        TIMER_D:cancel_timer(timer_id)
    end

    self:delete_temp("fighting")
    self:delete_temp("begin_time")
    self:delete_temp("players")
    self:delete_temp("sync_timer_id")
    spdlog.debug("combat", "clear combat : %s", self:get_id())
end

---! 广播
function COMBAT_OB:brocast_packet(packet_name, packet_data, exclude_user_ids)
    local players = self:get_player_obs()
    return USER_D:brocast_packet(players, packet_name, packet_data, exclude_user_ids)
end
